//
//  LGAddPatientGroupController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/3.
//  添加、修改分组

#import "LGAddPatientGroupController.h"
#import "LGPatientGroupCell.h"
@interface LGAddPatientGroupController ()<UITableViewDataSource,UITableViewDelegate>

/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;

@end

@implementation LGAddPatientGroupController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"添加/修改分组";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.sureBtn];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.group/groupWithPatient" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无分组" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
}

-(void)savePatientGroup{
    NSString *groupids = @"";
    for (NSDictionary *tempDic in self.dataArray) {
        if([LGNSString(tempDic[@"is_checked"]) isEqualToString:@"1"]){
            groupids = (groupids.length == 0)?LGNSString(tempDic[@"group_id"]):[NSString stringWithFormat:@"%@,%@",groupids,tempDic[@"group_id"]];
        }
    }
    [RequestUtil withPOST:@"/doctor/patient.group/patientGroupUpdate" parameters:@{@"patient_id":self.patientid,@"group_ids":groupids} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"保存成功" offset:0];
            if (self.patientGroupUpdate) {
                self.patientGroupUpdate();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    cell.titleLabel.text = LGNSString(tempDic[@"title"]);
    cell.selectBtn.selected = [LGNSString(tempDic[@"is_checked"]) isEqualToString:@"0"]?NO:YES;
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:self.dataArray[indexPath.row]];
    if([LGNSString(tempDic[@"is_checked"]) isEqualToString:@"0"]){
        [tempDic setValue:@"1" forKey:@"is_checked"];
    }else{
        [tempDic setValue:@"0" forKey:@"is_checked"];
    }
    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:tempDic];
    [self.tableView reloadData];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGCornerHeaderView *headerView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    headerView.topMargin = viewPix(10);
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footerView.bottomMargin = viewPix(10);
    return footerView;
}

-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(90)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientGroupCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(56);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_sureBtn addTarget:self action:@selector(savePatientGroup) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
